package com.hcss.core.common.mailmgt.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hcss.core.common.mailmgt.model.MailModel;
import com.hcss.core.common.mailmgt.model.SystemMailModel;
import com.hcss.core.common.mailmgt.service.MailSenderService;
import com.hcss.core.common.mailmgt.service.MailSetService;
import com.hcss.core.mvc.BaseController;




/** 
 * @ClassName: mailSetting 
 * @Description: TODO 系统设置
 * @author lvfang
 * @date 2013-4-16 下午2:56:00 
 *  
 */
@Controller
@RequestMapping("/mailSetting")
public class MailSetController extends BaseController{
	@Autowired
	private MailSetService mailSetService;
	@Autowired
	private MailSenderService mailSenderService;
	/**
	 * 系统设置首页
	 * @param dep 输入参数 
	 * @return 提示信息
	 * @throws Exception
	 */
	@RequestMapping("/mailSet")
	public ModelAndView index() throws Exception{
		SystemMailModel model = mailSetService.findSysModel();
		ModelAndView view = new ModelAndView("mailSet/mailSet");
		view.addObject("model", model);
		return view;
	}
	/**
	 * 系统设置
	 * @param dep 输入参数 
	 * @return 提示信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/setInfo", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> setInfo(@ModelAttribute("systemModel") SystemMailModel systemModel) 
			throws Exception{
		return getMsgTip(mailSetService.setInfo(systemModel));
	}
	
	/**
	 * 进入邮件模板设计页面
	 * @param dep 输入参数 
	 * @return 提示信息
	 * @throws Exception
	 */
	@RequestMapping("/mailTemplateIndex")
	public ModelAndView mailTemplateIndex() throws Exception{
		SystemMailModel model = mailSetService.findSysModel();
		ModelAndView view = new ModelAndView("mailTemplate/mailTemplateIndex");
		view.addObject("model", model);
		return view;
	}
	
	/**
	 * @Title: updateMailTemplate
	 * <p>修改邮件模板<p>
	 * @param systemModel
	 * @return
	 */
	@RequestMapping(value = "/updateMailTemplate", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> updateMailTemplate(SystemMailModel systemModel){
		return getMsgTip(mailSetService.updateMailTemplate(systemModel));
	}
	/** 
	* @Title: sendInfoTest 
	* @Description: TODO 测试邮件发送
	* @param email
	* @return
	* @throws Exception
	* @throws 
	*/ 
	@RequestMapping(value = "/sendInfoTest", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> sendInfoTest(@ModelAttribute("email") MailModel email) 
			throws Exception{
		String mailSubject = email.getSubObject();
		String mailCotent = email.getEmailContent();
		String[] toAddress = {email.getAddressee()};
		String[] ccAddress = null;
		if(!"".equals(email.getCc())){
			ccAddress = new String[1];
			ccAddress[0] = email.getCc();
		}
		int state = mailSenderService.sendSynchronizationMail(mailSubject, mailCotent, toAddress, ccAddress);
		return getMsgTip(state);
	}
	
	/** 
	* @Title: sendInfoArray 
	* @Description: TODO 测试异步发送邮件  批量
	* @return
	* @throws Exception
	* @throws 
	*/ 
	@RequestMapping(value = "/sendInfo", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> sendInfoArray() 
			throws Exception{
		String mailSubject = "JAVA测试邮件发送（标题）";
		String mailCotent = "<font color=red>JAVA测试内容！JAVA测试内容！JAVA测试内容！JAVA测试内容！</font>";
		String[] toAddress = {"lvfang@chinasofti.com","lvfang@chinasofti.com","lvfang@chinasofti.com","tanqiang@chinasofti.com"};
		String[] ccAddress = {"lvfang@chinasofti.com","lvfang@chinasofti.com"};
		int state = mailSenderService.sendMuptMail(mailSubject, mailCotent, toAddress, ccAddress);
		return getMsgTip(state);
	}
}
